package com.homework.HomeAcount.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.homework.HomeAcount.entity.Menu;
import com.homework.HomeAcount.entity.TreeNode;
import com.homework.HomeAcount.service.MenuService;

@Controller
@RequestMapping("/menu")
public class MenuController {

	@Autowired
	private MenuService menuService;
	
	@RequestMapping("/menuPage")
	@RequiresPermissions("menu:*")
	public String loadMenus(Model model) {
		List<Map<String, Object>> list = menuService.getParentMenus();
		model.addAttribute("pMenus", list);
		return "/menu/menu";
	}
	
	@RequestMapping("/loadMenus")
	@ResponseBody
	public List<TreeNode> loadMenus(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		return menuService.getMenus(map);
	}
	
	@RequestMapping("/getMenuInfoById")
	public @ResponseBody Menu getMenuInfoById(String id) {
		return menuService.getMenuInfoById(id);
	}
	
	@RequestMapping("/menuSave")
	public @ResponseBody Map<String, Object> menuSave(Menu menu,String opFlag) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if("1".equals(opFlag)) {
				menuService.insertMenuInfo(menu);
			}else {
				menuService.updateMenuById(menu);
			}
			List<Map<String, Object>> list = menuService.getParentMenus();
			map.put("pMenus", list);
			map.put("pMenu", menu.getParentid());
			map.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", false);
		}
		return map;
	}
}
